package ldh.common.testui.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import ldh.common.testui.constant.ParamCategory;
import ldh.common.testui.constant.TreeNodeType;
import ldh.common.testui.model.ParamModel;
import ldh.common.testui.model.TreeNode;
import ldh.common.testui.util.DataUtil;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

/**
 * Created by ldh123 on 2017/6/5.
 */
public class SqlDataPaneController implements Initializable{

    @FXML private RadioButton sqlTextBtn;
    @FXML private RadioButton sqlFileBtn;
    @FXML private RadioButton sqlCvsBtn;
    @FXML private TextArea sqlTextArea;
    @FXML private ListView<String> sqlFile;
    @FXML private ChoiceBox<String> databaseChoiceBox;
    @FXML private BorderPane sqlFilePane;
    @FXML private BorderPane sqlCvsPane;
    @FXML private CheckBox baseOffsetDayCheckBox;
    @FXML private DatePicker baseOffsetDayDatePicker;

    private ToggleGroup toggleGroup = new ToggleGroup();
    private TreeItem<TreeNode> treeItem;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        sqlTextBtn.setToggleGroup(toggleGroup);
        sqlFileBtn.setToggleGroup(toggleGroup);
        sqlCvsBtn.setToggleGroup(toggleGroup);

    }

    public void setTreeItem(TreeItem<TreeNode> treeItem) {
        this.treeItem = treeItem;
        List<String> database = new ArrayList();
        List<ParamModel> paramModels = DataUtil.getAllParamModels(treeItem, ParamCategory.Database);
        database = paramModels.stream().map(pm->pm.getName()).collect(Collectors.toList());
        databaseChoiceBox.getItems().addAll(database);
    }

    private TreeItem<TreeNode> getParamTreeItem(TreeItem<TreeNode> treeItem) {
        for (TreeItem<TreeNode> treeNodeTreeItem : treeItem.getChildren()) {
            if(treeNodeTreeItem.getValue().getTreeNodeType() == TreeNodeType.Param) {
                return treeNodeTreeItem;
            }
        }
        return null;
    }

    public void selectFilePane(ActionEvent actionEvent) {
        sqlTextArea.setVisible(false);
        sqlFilePane.setVisible(true);
        sqlCvsPane.setVisible(false);
    }

    public void selectSqlTextPane(ActionEvent actionEvent) {
        sqlTextArea.setVisible(true);
        sqlFilePane.setVisible(false);
        sqlCvsPane.setVisible(false);
    }

    public void selectCvsPane(ActionEvent actionEvent) {
        sqlTextArea.setVisible(false);
        sqlFilePane.setVisible(false);
        sqlCvsPane.setVisible(true);
    }

    public void addFile(ActionEvent actionEvent) {
    }

    public void deleteFile(ActionEvent actionEvent) {
    }

    public void save(ActionEvent actionEvent) {
    }


    public void isBaseOffsetDay(ActionEvent actionEvent) {
        if (baseOffsetDayCheckBox.isSelected()) {
            baseOffsetDayDatePicker.setDisable(false);
        } else {
            baseOffsetDayDatePicker.setDisable(true);
        }
    }
}
